package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.bsedu.BseduCheckDTO;
import com.xhwl.centiotdata.pojo.dto.bsedu.BseduVisitorDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IDeviceProductService;
import com.xhwl.centiotdata.service.cent.IInfoService;
import com.xhwl.centiotdata.service.cent.IPedestrianCentService;
import com.xhwl.centiotdata.suppliersystem.bsedu.DeviceBseduHandler;
import com.xhwl.common.dto.cent.pedes.BseduCheckDataDTO;
import com.xhwl.common.dto.cent.pedes.BseduVistorsDataDTO;
import com.xhwl.common.pojo.cent.device.DeviceProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * @Author： tangwei
 * @Description：对接金茂校园的handler
 * @Date 2022/08/11/ 09:44
 */
@Service
@Slf4j
public class PedestrianIntegrationBseDuHandler extends PedestrianIntegrationHandler {

    @Autowired
    private DeviceBseduHandler deviceBseduHandler;

    @Autowired
    private IPedestrianCentService pedestrianCentService;

    @Autowired
    private IInfoService iInfoService;

    @Autowired
    private IDeviceProductService iDeviceProductService;

    @Override
    public void afterPropertiesSet() throws Exception {
        List<DeviceProduct> productList = iDeviceProductService.list();
        AtomicInteger productId = new AtomicInteger(51);
        for(DeviceProduct product : productList){
            if(product.getName().contains("数字哨兵")){
                productId.set(product.getId());
                break;
            }
        };
        PedestrianIntegrationFactory.register(productId.get(), this);
    }

    /*
     * @author tangwei
     * @description 手动触发拉取查验记录
     * */
    @Override
    public void pullCheckData(PedestrianIntegrationConfigDTO configDTO) throws Exception {
        // 拉取数据
        List<BseduCheckDTO> checkDataList = deviceBseduHandler.getCheck(configDTO);
        if (CollectionUtils.isEmpty(checkDataList)) {
            return;
        }
        // 处理数据
        List<BseduCheckDataDTO> RecordDTOList = this.handleCheckData(configDTO, checkDataList);
        log.info("处理数据:{}", RecordDTOList);
        // 转存数据
        pedestrianCentService.saveCheckData(RecordDTOList);
    }

    private List<BseduCheckDataDTO> handleCheckData(PedestrianIntegrationConfigDTO configDTO, List<BseduCheckDTO> recordBseDuDTOList) {
        List<BseduCheckDataDTO> BseduCheckDataDTOList = new ArrayList<BseduCheckDataDTO>(recordBseDuDTOList.size());
        for (BseduCheckDTO bseduCheckDTO : recordBseDuDTOList) {
            BseduCheckDataDTO bseduCheckDataDTO = BseduCheckDataDTO.builder()
                    .iDCardNo(bseduCheckDTO.getIDCardNo())
                    .visitorName(bseduCheckDTO.getVisitorName())
                    .visitorType(bseduCheckDTO.getVisitorType())
                    .bodyTemper(bseduCheckDTO.getBodyTemper())
                    .signInTime(bseduCheckDTO.getSignInTime())
                    .signOutTime(bseduCheckDTO.getSignOutTime())
                    .openId(bseduCheckDTO.getId())
                    .projectId(configDTO.getProjectId())
                    .result(bseduCheckDTO.getResult()).build();
            BseduCheckDataDTOList.add(bseduCheckDataDTO);
        }
        return BseduCheckDataDTOList;
    }


    public void pullVisitorData(Integer intervalDays,PedestrianIntegrationConfigDTO configDTO) throws Exception {
        // 拉取数据
        List<BseduVisitorDTO> visitorDataList = deviceBseduHandler.getVisitors(intervalDays,configDTO);
        log.info("访客数据为{}", visitorDataList);
        if (CollectionUtils.isEmpty(visitorDataList)) { return; }
        // 处理数据
        List<BseduVistorsDataDTO> recordDTOList = this.handleVisitorsData(configDTO,visitorDataList);
        iInfoService.openSaveBatch(recordDTOList);
    }
    private List<BseduVistorsDataDTO> handleVisitorsData(PedestrianIntegrationConfigDTO configDTO, List<BseduVisitorDTO> visitorDataList) {
        List<BseduVistorsDataDTO> BseduVisitorsDTOList = new ArrayList<BseduVistorsDataDTO>(visitorDataList.size());
        for (BseduVisitorDTO bseduVisitorDTO : visitorDataList){
            String bookingDate = bseduVisitorDTO.getBookingDate();
            String bookingTime = bseduVisitorDTO.getBookingTime()+":00";
            // 时间格式化处理
            if(bookingTime.contains("-")){
                String[] time = bookingTime.split("-");
                String authStartTime = bookingDate.concat(" "+time[0]);
                String authEndTime = bookingDate.concat(" "+time[1]);
                BseduVistorsDataDTO bseduVisitor = BseduVistorsDataDTO.builder()
                        .visitorName(bseduVisitorDTO.getVisitorName())
                        .projectId(configDTO.getProjectId())
                        .openId(bseduVisitorDTO.getId())
                        .idno(bseduVisitorDTO.getIdno())
                        .carNo(bseduVisitorDTO.getCarNo())
                        .bookingDate(authStartTime)
                        .bookingTime(authEndTime).build();
                BseduVisitorsDTOList.add(bseduVisitor);
            }else{
                BseduVistorsDataDTO bseduVisitor = BseduVistorsDataDTO.builder()
                        .visitorName(bseduVisitorDTO.getVisitorName())
                        .projectId(configDTO.getProjectId())
                        .openId(bseduVisitorDTO.getId())
                        .idno(bseduVisitorDTO.getIdno())
                        .carNo(bseduVisitorDTO.getCarNo())
                        .bookingDate(bseduVisitorDTO.getBookingDate().concat(" "+bookingTime))
                        .bookingTime(bseduVisitorDTO.getBookingDate().concat(" "+bookingTime))
                        .build();
                BseduVisitorsDTOList.add(bseduVisitor);
            }
        }
        return BseduVisitorsDTOList;
    }
}
